const BASE_URL = 'http://192.168.31.211:8101/api'; // 替换为你的API域名


export function request(options) {
    // const BASE_URL = 'http://localhost:8101/api';
    // const BASE_URL = 'http://192.168.31.211:8101/api';
    // const BASE_URL = 'http://172.20.10.3:8101/api';
    const cookies = wx.getStorageSync('Cookie') || {};
    const cookieStr = Object.keys(cookies).map(key => `${key}=${cookies[key]}`).join('; ');

    const {url, from, onChunkReceived} = options;

    const newOptions = {
        ...options,
        header: {
            ...options.header,
            'Cookie': cookieStr
        },
        url: BASE_URL + url
    };

    const originalRequest = wx.request;

    return new Promise((resolve, reject) => {
        const request = originalRequest({
            ...newOptions,
            success: (res) => {
                // 检查响应状态码或返回的数据
                if (res.data.code === 40100) {
                    // 用户未登录，跳转到登录页面
                    const url = from ? `/pages/login/login?from=${from}` : '/pages/login/login';
                    wx.navigateTo({
                        url
                    });
                    reject(res);
                } else {
                    resolve(res);
                }
            },

            fail: function (err) {
                reject(err);
            }
        });
        if (onChunkReceived) {
            request.onChunkReceived(onChunkReceived)
        }
    });
}

